package com.tanhua.sso.controller;

import com.tanhua.sso.service.Sendsms;
import com.tanhua.sso.tool.smsTool;
import com.tanhua.sso.vo.ErrorResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 发送短信的业务层
 */
@RestController
@RequestMapping("user")
@Slf4j
public class SendController {
    @Autowired
    Sendsms sendsms;

    @PostMapping("login")
    public ResponseEntity<ErrorResult> sendCheckCode(@RequestBody Map<String,String>param){
        ErrorResult errorResult=null;
        //请求体为手机号，获取手机号，通过下一步的发送短信的方法，将手机号参数传入其中
        String phone = param.get("phone");
        //调用业务层中的发送短信的方法

        try {
            errorResult = this.sendsms.sendCheckCode(phone);
            if (null==errorResult){
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            //打印一个日志，如果发送失败就输出一段话，同时打印手机号和异常信息
            log.error("发送短信验证码失败~ phone = "+phone,e);
            ErrorResult.builder().errCode("000002").errMessage("短信验证码发送失败").build();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }
}
